<?php


namespace app\api\controller;


use think\Controller;
use think\Db;
use think\Exception;
use think\Session;


class Tickets extends Controller
{

    //获取票种
    public function typeApi(){
        $common = new Common();
        try{
            $data = $common->publicAttribute("tickets_type");

            $dataVal = [];
            foreach ($data as $k=>$v){
                $dataVal[] = ['id'=>$k,'title'=>$v];
            }

            $black['code'] = 0;
            $black['data'] = $dataVal;
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }

    /**
     * 获取票种
     * @author zhixuedong
     * @date 2021/1/21/021
     */
    public function listsApi(){
        $common = new Common();
        try {
            $appid = $_POST['appid'];
            $config = Db::name("config")->where(['xcx_appid'=>$appid])->find();
            Session::set("thisWechat",$config);
            if(!empty($_POST['type'])){
                $where['type'] = $_POST['type'];
            }
            $where['uniacid'] = $config['id'];
            $where['status'] = 1;
            $page = empty($_POST['page']) ? 1 : $_GET['page'];
            $limit = empty($_POST['limit']) ? 10 : $_GET['limit'];
            $start = ($page - 1) * $limit;
            $publicTable = "tickets_tickets";
            $field="id,title,thumb,type,price,inventory,soldout";
            $newSql = $common->public_select($publicTable,$where,$field,$start,$limit);
            $list = $newSql->order("sort desc,id desc")->select();

            $tickets_type = $common->publicAttribute("tickets_type");
            foreach ($list as $k=>$v){
                $list[$k]['type'] = $tickets_type[$v['type']];
            }

            $count = $common->public_select($publicTable,$where)->count("*");
            $black['code'] = 0;
            $black['data'] = $list;
            $black['count'] = $count;
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }

    /**
     * 票详情
     * @return false|string
     * @author zhixuedong
     * @date 2021/1/21/021
     */
    public function detailApi(){
        $common = new Common();
        try {
            $where['id'] = $_POST['id'];
            $publicTable = "tickets_tickets";
            $field="id,title,thumb,type,price,inventory,soldout,validity,degree,time_range,address,content";
            $newSql = $common->public_select($publicTable,$where,$field);
            $detail = $newSql->order("sort desc,id desc")->find();

            $tickets_type = $common->publicAttribute("tickets_type");
            $detail['type'] = $tickets_type[$detail['type']];

            $black['code'] = 0;
            $black['data'] = $detail;
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }


}